#ifndef MONIER_SE_DICT_H
#define MONIER_SE_DICT_H
#include <QFile>
#include <QMultiHash>
#include <QPair>
#include <QString>
#include <QStringList>
#include <QComboBox>
#include <QSettings>

#include "dictionaryinterface.h"

class Transliterator;

//to store the offset(position) and length;
typedef QPair<qint64, int> MonierSEPosition;

typedef QMultiHash<QString, MonierSEPosition > MonierSEIndex;
typedef QMultiHash<QString, MonierSEPosition >::iterator MonierSEIndexIterator;
typedef QList<MonierSEPosition> MonierSEItemList;

//to store the ref number in tail section.
//some words only had the content point to ref number.
//we will translate it to words when we rendering it.
typedef QMultiHash<QString, QString> MonierSERef;

//store the pagenumber and scanjpglink.
typedef QMultiHash<QString, QString> MonierSEScan;

//store greek words
typedef QMultiHash<QString, QString> MonierSEGreek;

typedef QMultiHash<QString, QPair<QString, QString> > WorksAndAuthors;


class MonierSEDict : public QObject, DictionaryInterface
{
    Q_OBJECT
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    Q_PLUGIN_METADATA(IID "Abhidhaana.Plugin.DictionaryInterface/1.0" FILE "moniersedict.json")
#endif
    Q_INTERFACES(DictionaryInterface)


public:
    MonierSEDict();
    ~MonierSEDict();

    void queryWord(QString key);

    QString getTooltip(QString protocol, QString key);

    QWidget *getToolbarFilterWidget();

    DictionaryDescription getDescription();

    QString getListFontName(){return "Sanskrit 2003";}

    QList<QPair<QString, QString> > getCandidateWords(QString key);

private:
    QString getRefWord(QString lnum);

    QString getScanJpgLink(QString page);

    QString getGreekWord(QString key);

    QString processLine(QString word);

    QString markup_devanagari(QString string);
    QString markup_key2(QString key2) {return markup_devanagari(key2);}

    QString markup_pagecolumn(QString pc);

    QString markup_body(QString string);

    QStringList findWords(QString key);
    QString readMonierSEContent(qint64 pos, int offset);



    MonierSEItemList findMonierSEIndex(QString key);

    QPair<MonierSEIndex, MonierSERef> createMonierSEIndex(QString idxfile);

    MonierSEScan createMonierSEScanIdx(QString scanfile);
    MonierSEGreek createMonierSEGreekIdx(QString greekfile);
    WorksAndAuthors createWorksAndAuthorsIdx(QString wnafile);

    MonierSEIndex idx;
    MonierSERef   ref;
    MonierSEScan  scanidx;
    MonierSEGreek greekidx;

    WorksAndAuthors wnaidx;

    QFile *monierXml;
    QStringList allkey;

    QString keyword;

    Transliterator *transliterator;


    //as ToolbarFilterWidget;
    QComboBox  *transCombo;
    QString textToSlp1(QString text);
    QString slp1ToText(QString key);

    QString slp1ToDeva(QString slp1);


    QStringList slp1ListToDevaList(QStringList slp1List);

    QSettings *settings;

public slots:
    void slotSaveTransliteration(int i);

signals:
    void filterChanged();
    void readyToShow(QString);
};


#endif // MONIER_SE_DICT_H
